package com.biz.primus.ms.cart.service.builder;


import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.model.cart.enums.ShippingMessageTipEnum;
import com.biz.primus.model.cart.exception.ShopCartException;
import com.biz.primus.model.cart.vo.ShopCartItemVo;
import com.biz.primus.model.cart.vo.ShopCartProductRespVo;

/**
 * 购物车商品bundle builder
 *
 * @author JKLiues
 * @date 2017/11/2
 */
public class ShopCartItemVoBuilder {
    private ShopCartItemVoBuilder() {
    }

    private ShopCartItemVo shopCartItemVo;

    public static ShopCartItemVoBuilder createBuilder(ShopCartProductRespVo cartProductRespVo) {
        ShopCartItemVoBuilder builder = new ShopCartItemVoBuilder();
        AssertUtils.notNull(cartProductRespVo.getProductCode(), ShopCartException.PRODUCT_CODE_IS_NULL);
        AssertUtils.notNull(cartProductRespVo.getProductName(), ShopCartException.PRODUCT_NAME_IS_NULL);
        AssertUtils.notNull(cartProductRespVo.getLogo(), ShopCartException.PRODUCT_LOGO_IS_NULL);
        builder.shopCartItemVo = new ShopCartItemVo();
        builder.shopCartItemVo.setProductCode(cartProductRespVo.getProductCode());
        builder.shopCartItemVo.setName(cartProductRespVo.getProductName());
        builder.shopCartItemVo.setSubTitle(cartProductRespVo.getSubTitle());
        builder.shopCartItemVo.setLogo(cartProductRespVo.getLogo());

        builder.shopCartItemVo.setPrice(cartProductRespVo.getFinalPrice());
        builder.shopCartItemVo.setMarketPrice(cartProductRespVo.getMarketPrice());
        switch (cartProductRespVo.getStatus()) {
            case OFF_SALE:
                builder.shopCartItemVo.setMessage(ShippingMessageTipEnum.OFF_SALE.getDescription());
                builder.shopCartItemVo.setCanBuy(false);
                break;
            default:
                break;

        }
        return builder;
    }

    public ShopCartItemVoBuilder setFinalPrice(Integer finalPrice) {
        AssertUtils.notNull(finalPrice, ShopCartException.PRICE_IS_NULL);
        shopCartItemVo.setPrice(finalPrice);
        return this;
    }

    public ShopCartItemVoBuilder setSelected(boolean selected) {
        shopCartItemVo.setSelected(selected);
        return this;
    }


    public ShopCartItemVoBuilder setMarketPrice(Integer marketPrice) {
        AssertUtils.notNull(marketPrice, ShopCartException.PRICE_IS_NULL);
        shopCartItemVo.setPrice(marketPrice);
        return this;
    }

    /**
     * 设置该商品购物车总数
     *
     * @param quantity 商品总数
     * @return
     */
    public ShopCartItemVoBuilder setQuantity(Integer quantity) {
        AssertUtils.isTrue(ValueUtils.getValue(quantity) > 0, ShopCartException.PRODUCT_QUANTITY_LT_ZERO);
        shopCartItemVo.setAmount(quantity);
        return this;
    }

    public ShopCartItemVo build() {
        return shopCartItemVo;
    }

}
